// Description: Product Integration Trigger for automatically activating and replicating
//   	  			new products into the SF PriceBook tables for each active currency.
//   	  			If a baseprice is supplied the trigger will use the current currency exchange rates to set 
//  	  			the unit price for each pricebook entry record.
//   	  			WARNING: The trigger will overwrite existing Price Book Entry's for Products that
//   	  			have already been loaded once.
//   Author(s): S.Gare & R.Clark
//   Copyright: BrightGen Ltd (http://www.brightgen.com)
//   Date: 12/03/2008

trigger Product_AfterInsert_PriceBookEntry on Product2 (after insert) {

    // List of Products that have been inserted (Trigger.NewMap)
    Map<ID,Product2> InsertedProducts = trigger.NewMap;

    // Delete the existing price book entry records for the products that are being inserted
    PriceBookEntry[] ExistingPBE = [SELECT ID 
                                      FROM PriceBookEntry 
                                     WHERE Product2ID IN :Insertedproducts.keySet()];
    
    Delete ExistingPBE;
    
    // Get all the Currencies - Needs the Org to have multi-currencies enabled
    // otherwise you'll need to comment out all currency SOQL statements and references
    CurrencyType[] ActiveCurrencies = [SELECT c.IsoCode, c.ConversionRate 
                                         FROM CurrencyType c 
                                        WHERE c.IsActive = True];
     
    // Assuming you only use the Standard Price Book get its ID, otherwise you need to create an
    // additional loop to iterate through the ID of all Pricebooks to be updated.    
    ID StdPriceBookID = [SELECT p.Id From Pricebook2 p
    					  WHERE p.IsStandard = True][0].ID;
    
    // Create Empty PriceBook Array
    PriceBookEntry[] NewPBE = new PriceBookEntry[]{};
    
    for(Product2 p: trigger.new){
    
        // If not using multi-currency then replace the block below with the following line instead 
        // NewPBE.add(new PriceBookEntry(Product2ID=p.ID, PriceBook2ID=StdPriceBookID, IsActive=True, UnitPrice=0, UseStandardPrice=False));  

    
        // Loop through each currency and insert a new price book entry for each
        // In this version of the package the base price is left as 0, but if you are importing a standard price
        // you could easily set a currency unit price based on your current exchange rate.
        for(CurrencyType curcode: ActiveCurrencies){
            // create PBE Object
            NewPBE.add(new PriceBookEntry(Product2ID=p.ID, 
                                          PriceBook2ID=StdPriceBookID, 
                                          CurrencyIsoCode=curCode.IsoCode, 
                                          IsActive=True, 
                                          UnitPrice=p.SourceBasePrice__c * curcode.ConversionRate, 
                                          UseStandardPrice=False
                                         )
                      );  
            
        }     
    }
    
    // Once the array of PriceBookEntry records is ready insert it
    Insert NewPBE;

}